﻿Imports Microsoft.Win32

Public Class Common

#Region "Constants"
    Private Const eventLogName As String = "Application"
    Private Const softwareSubKey As String = "MtechSolutions.ie\BizUnitTestRunner"
    Private Const registrySubKey As String = "Software\" & softwareSubKey

    Friend Const formCaption As String = "BizUnit Test Runner"

#End Region

#Region "Registry Helper"

    ''' <summary>
    ''' Creates a new sub key under the Software key within the registry
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Shared Function CreateSubKey(ByVal subKey As String) As RegistryKey
        Dim regKey As RegistryKey
        Dim retVal As RegistryKey = Nothing

        Try
            regKey = Registry.LocalMachine.OpenSubKey(registrySubKey, False)

            If regKey Is Nothing Then
                regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
                regKey.OpenSubKey(registrySubKey)
                If regKey IsNot Nothing Then
                    regKey.CreateSubKey(subKey)
                    retVal = regKey.OpenSubKey(registrySubKey)
                Else
                    LogError("Can't open the Software key within the registry. Please check your security settings", True)
                    retVal = Nothing
                End If
            End If

        Catch ex As Exception
            LogError("Error creating SubKey in registry: " & ex.Message, True)
        Finally
            regKey = Nothing
        End Try

        Return retVal
    End Function

    ''' <summary>
    ''' Returns a value for the key from the PulseInstall Subkey
    ''' </summary>
    ''' <param name="keyName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Shared Function ReadRegistry(ByVal keyName As String) As String
        Dim regKey As RegistryKey
        Dim retVal As String = String.Empty

        Try
            regKey = Registry.LocalMachine.OpenSubKey(registrySubKey, False)
            If regKey Is Nothing Then regKey = CreateSubKey(softwareSubKey)

            If regKey IsNot Nothing Then
                retVal = CType(regKey.GetValue(keyName, ""), String)
                regKey.Close()
            End If
        Catch ex As Exception
            LogError("Error reading registry (" & keyName & "):" & ex.Message.ToString, True)
        Finally
            regKey = Nothing
        End Try

        Return retVal

    End Function

    ''' <summary>
    ''' Sets a key value under the PulseInstall subkey
    ''' </summary>
    ''' <param name="keyName"></param>
    ''' <param name="keyValue"></param>
    ''' <remarks></remarks>
    Friend Shared Sub WriteRegistry(ByVal keyName As String, ByVal keyValue As String)
        Dim regKey As RegistryKey

        Try
            regKey = Registry.LocalMachine.OpenSubKey(registrySubKey, True)
            regKey.SetValue(keyName, keyValue)

            regKey.Close()
        Catch ex As Exception
            LogError("Error writing to registry (" & keyName & "):" & ex.Message.ToString, True)
        Finally
            regKey = Nothing
        End Try

    End Sub

#End Region

#Region "Log Helper"

    ''' <summary>
    ''' Logs the error and then display the error to user if displayMessage is flag switched on
    ''' </summary>
    ''' <param name="message">Message that will be logged and/or displayed to user</param>
    ''' <param name="displayMessage">Value indicates if message is to be displayed to user or not</param>
    ''' <remarks></remarks>
    Friend Shared Sub LogError(ByVal message As String, ByVal displayMessage As Boolean)
        LogError(message, displayMessage, formCaption, MessageBoxIcon.Error)
    End Sub

    ''' <summary>
    ''' Logs the error to the event log and then displays the error to user if displayMessage is flag switched on
    ''' </summary>
    ''' <param name="message">Message that will be logged and/or displayed to user</param>
    ''' <param name="displayMessage">Value indicates if message is to be displayed to user or not</param>    
    ''' <param name="caption">Caption to be displayed on the messagebox</param>
    ''' <param name="messageBoxIcon">Icon to be displayed on the messagebox</param>
    ''' <remarks></remarks>
    Friend Shared Sub LogError(ByVal message As String, ByVal displayMessage As Boolean, ByVal caption As String, ByVal messageBoxIcon As MessageBoxIcon)

        Dim myLog As New EventLog
        myLog.Source = formCaption
        If Not EventLog.SourceExists(myLog.Source) Then EventLog.CreateEventSource(myLog.Source, eventLogName)

        myLog.WriteEntry(message, EventLogEntryType.Warning)

        If displayMessage Then
            MessageBox.Show(message, caption, MessageBoxButtons.OK, messageBoxIcon, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
        End If
    End Sub

#End Region

End Class
